package cn.shyouchi.wuguanjia.android.widget.dialog;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.ui.event.labor.RankTime;

/**
 * Created by Eenie on 2016/11/21 at 10:19
 * Email: 472279981@qq.com
 * Des:
 */

public class LaborEditAdapter extends BaseQuickAdapter<RankTime.ResultMessageBean, BaseViewHolder> {

    public LaborEditAdapter(List<RankTime.ResultMessageBean> data) {
        super(R.layout.item_labor_edit_layout, data);
    }

    @Override
    protected void convert(final BaseViewHolder holder, final RankTime.ResultMessageBean entity) {
        holder.setText(R.id.ckl_labor_name, entity.getServicesname());
        final TextView cb = holder.getView(R.id.ckl_labor_name);
        holder.getView(R.id.edit_labor_count).setEnabled(entity.isChecked());

        holder.setText(R.id.edit_labor_count, entity.isChecked() && entity.getCount() != 0 ? String.valueOf(entity.getCount()) : "");
        final EditText et = holder.getView(R.id.edit_labor_count);

        if (entity.isChecked()) {
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                               public void run() {
                                   InputMethodManager inputManager =
                                           (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                                   inputManager.showSoftInput(et, 0);
                               }

                           },
                    300);
        }

        et.setFocusable(true);
        et.setFocusableInTouchMode(true);
        et.requestFocus();
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString())) {
                    getData().get(holder.getPosition()).setCount(Integer.parseInt(s.toString()));
                } else {
                    getData().get(holder.getPosition()).setCount(0);
//                    entity.setCount(0);
                }
            }
        });
        cb.setSelected(entity.isChecked());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                entity.setChecked(!entity.isChecked());
                notifyItemChanged(holder.getPosition());
            }
        });
    }


    public List<RankTime.ResultMessageBean> getSelData() {
        List<RankTime.ResultMessageBean> mLaborEntities = new ArrayList<>();
        for (RankTime.ResultMessageBean bean : getData()) {
            if (bean.isChecked() && bean.getCount() > 0) {
                mLaborEntities.add(bean);
            }
        }
        return mLaborEntities;
    }
}
